﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Ztest
{
   public static class SqlDAL
    {
        /// <summary>
        /// 根据用户ID获取卡券
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public static DataTable GetHospitalList(DateTime betime, DateTime endtime)
        {
            string sql = @"select * from code_table ";
           
                sql += "where data_time >=@betime and data_time<@endtime";


            // 创建一个SqlParameter数组
            SqlParameter[] parameters = new SqlParameter[2];

            // 为数组中的每个SqlParameter分配值
            parameters[0] = new SqlParameter("@betime", SqlDbType.DateTime) { Value = betime }; // 替换value1为您需要的值
            parameters[1] = new SqlParameter("@endtime", SqlDbType.DateTime) { Value = endtime }; // 替换123为您需要的值

            DataTable tab = SqlDbHelper.ExecuteDataTable(sql, parameters);
            if (tab!=null)
            {
                return tab;
            }
            return null;
           
        }

        /// <summary>
        /// 根据用户ID获取卡券
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public static bool  GetCode(string code)
        {
            string sql = @"select * from code_table ";

            sql += "where bar_code =@bar_code ";


            // 创建一个SqlParameter数组
            SqlParameter[] parameters = new SqlParameter[1];

            // 为数组中的每个SqlParameter分配值
            parameters[0] = new SqlParameter("@bar_code", SqlDbType.VarChar) { Value = code }; // 替换value1为您需要的值
            DataTable tab = SqlDbHelper.ExecuteDataTable(sql, parameters);
            if (tab == null)
            {
                return true;
            }
            if (tab.Rows.Count==0)
            {
                return true;
            }
            return false;

        }

        /// <summary>
        /// 根据用户ID获取卡券
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public static int InsertCode(string Code)
        {
            string queryString = "INSERT INTO code_table (bar_code, data_time) VALUES (@barCode, @dataTime)";


            // 创建一个SqlParameter数组
            SqlParameter[] parameters = new SqlParameter[2];

            // 为数组中的每个SqlParameter分配值
            parameters[0] = new SqlParameter("@barCode", SqlDbType.VarChar) { Value = Code };
            parameters[1] = new SqlParameter("@dataTime", SqlDbType.DateTime) { Value = DateTime.Now };

            int tab = SqlDbHelper.ExecuteNonQuery(queryString, parameters);
           return tab;

        }

    }
}
